<?php
/*
 * 图像的水印的生成
 */
date_default_timezone_set('UTC');//date时间报错的处理
header('content-type:image/png');
#读取logo图片
$src_im = imagecreatefrompng('11.png');
#读取被水印的图片
$dst_im = imagecreatefrompng('demo.png');
#logo图的宽高
$logowidth = imagesx($src_im);
$logoheight = imagesy($src_im);
#被水印图的宽高
$imagewidth = imagesx($dst_im);
$imageheight = imagesy($dst_im);
#水印位置
$position = 1; # 1左上角 2右上角 3左下角 4右下角 5中间 
switch ($position) {
	case 1:
		$dst_x = 10;
		$dst_y = 10;
		break;
	case 2:
		$dst_x = $imagewidth - $logowidth -10;
		$dst_y = 10;
		break;
	case 3:
		$dst_x = 10;
		$dst_y = $imageheight- $logoheight - 10;
		break;
	case 4:
		$dst_x = $imagewidth - $logowidth - 10;
		$dst_y = $imageheight - $logoheight - 10;
		break;
	case 5:
		$dst_x = ($imagewidth - $logowidth)/2;
		$dst_y = ($imageheight - $logoheight)/2;
		break;	
}

#水印实现
imagecopymerge($dst_im, $src_im, $dst_x, $dst_y, 0, 0, $logowidth, $logoheight, $pct = 100);//$pct 水印的透明度

#设置水印后的名称
$ext = '.png';
$filename = date('Ymdhis').rand(0,9).$ext;
#输入水印结果
imagepng($dst_im,'./'.$filename);



//加水印
//封装成函数调用
function uploadTmageToMark($dst,$logo,$dir,$p=3,$pct=100)
{
	//判断水印图和被水印图是否是合法的图像
	$allType = array('image/jpeg','image/gif','image/png');
	$imageType1 = getimagesize($dst);
	$imageType2 = getimagesize($logo);
	if(in_array($imageType1['mine'],$allType) == false || in_array($imageType2['mime'],$allType) == false)
	{
		return false;
	}
	//判断$dst_dir指定的目录是否存在
	if(is_dir($dir) != true || file_exists($dir) != true)
	{
		return false;
	}
	//读取底图
	switch ($imageType1['mime']) {
		case 'image/jpeg':
			$dst_im = imagecreatefromjpeg($dst);
			break;
		case 'image/png':
			$dst_im = imagecreatefrompng($dst);
			break;
		case 'image/gif':
			$dst_im = imagecreatefromgif($dst);
			break;
	}
	//读取水印图
	switch ($imageType2['mime']) {
		case 'image/jpeg':
			$src_im = imagecreatefromjpeg($logo);
			break;
		case 'image/png':
			$src_im = imagecreatefrompng($logo);
			break;
		case 'image/gif':
			$src_im = imagecreatefromgif($logo);
			break;
	}
	//将logo放到底图上
	switch ($p) {
	case 1:
		$dst_x = 10;
		$dst_y = 10;
		break;
	case 2:
		$dst_x = $imagewidth - $logowidth -10;
		$dst_y = 10;
		break;
	case 3:
		$dst_x = 10;
		$dst_y = $imageheight- $logoheight - 10;
		break;
	case 4:
		$dst_x = $imagewidth - $logowidth - 10;
		$dst_y = $imageheight - $logoheight - 10;
		break;
	case 5:
		$dst_x = ($imagewidth - $logowidth)/2;
		$dst_y = ($imageheight - $logoheight)/2;
		break;	
	}
	//？？？？？？？？？？？？？？？未写完  需完善

	//水印实现
	imagecopymerge($dst_im, $src_im, $dst_x, $dst_y, 0, 0, $logowidth, $logoheight, $pct = 100);

	#输入水印结果
	imagepng($dst_im,'aaa.png');
}
?>